 import axios from "axios";

const instance = axios.create({
  timeout: 5000,
  headers: { 'Content-Type': 'application/json;charset=utf-8' },
});

const errorHandler = (status,info) => { 
    switch(status) {
        case 400:
            console.log('语义错误-400');
            break;
        case 401:
            console.log('服务器认证错误-401');
            break;
        case 403:
            console.log('服务器拒绝访问-403');
            break;
        case 404:
            console.log('请求资源不存在-404');
            break;
        case 500:
            console.log('服务器错误-500');
            break;
        case 502:
            console.log('服务器无响应-502');
            break;
        default:
            console.log("custom-error",info);
            break;
    }
};


//拦截器配置
instance.interceptors.request.use(
  (config) => {
    // if (config.method === "post") {

    // }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (response) => {
    return response.status === 200 ? Promise.resolve(response) : Promise.reject(response);
  },
  (error) => {
    const { response } = error;
    errorHandler(response.status,response.info);
    return Promise.reject(error);
  }
);

export default instance;